/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：User_recharge接口实现                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Repository.SqlServer                                  
*│　类    名： User_rechargeRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.DbHelper;
using NetCoreCMS.IRepository;
using NetCoreCMS.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using System.Transactions;
using System.Data;

namespace NetCoreCMS.Repository.SqlServer
{
    public class User_rechargeRepository:BaseRepository<User_recharge,Int32>, IUser_rechargeRepository
    {
        public User_rechargeRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption =options.Get("NetCoreCMS");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            //_dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }

        public bool Recharge(User_recharge model)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
                {

                    #region 增加一条账户余额记录===============
                    User_amount_log amountModel = new User_amount_log();
                    amountModel.user_id = model.user_id;
                    amountModel.user_name = model.user_name;
                    amountModel.value = model.amount;
                    amountModel.remark = "在线充值，单号：" + model.recharge_no;
                    _dbConnection.Insert<User_amount_log>(amountModel);
                    #endregion
                    int id = (int)_dbConnection.Insert<User_recharge>(model);
                    _dbConnection.Execute("update [" + nameof(Users) + "] set amount+=" + model.amount + " where id=" + model.user_id);
                    scope.Complete();
                    return id > 0;
                }
            }
        }
    }
}